package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.consigngeneralexception.NoConsignGeneralException;
import com.xinzhitong.www.exception.system.NullContactUsFileException;
import com.xinzhitong.www.exception.system.NullServiceAgreementException;
import com.xinzhitong.www.exception.system.OpinionTextIsNullException;
import com.xinzhitong.www.service.SystemService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统控制器
 *
 * @author 魏骆淳  钟伟良 吴展鹏
 */
@CrossOrigin
@RestController
@RequestMapping(value = "system")
public class SystemController {

    @Resource
    private SystemService systemService;

    /**
     * 发表意见
     *
     * @param opinionText    意见文本
     * @param opinionPicture 意见图片
     * @return 发表成功状态
     */
    @RequestMapping(value = "publishOpinion", method = RequestMethod.PUT)
    public Message opinionInformation(
            @RequestParam("opinionText") String opinionText,
            @RequestParam("opinionPicture") List<MultipartFile> opinionPicture//图片数量、、传回路径列表
    ) {
        String msg = "感谢您的反馈！";
        try {
            systemService.putOpinionInformation(opinionText, opinionPicture);
            return Message.createSuc(msg);
        } catch (OpinionTextIsNullException e) {
            e.printStackTrace();
            msg = "发表失败";
        }
        return Message.createErr(msg);
    }


    /**
     * 新发表意见
     *
     * @param opinionText      意见文本
     * @param opinionPictureNum  意见图片数量
     * @return 发表成功状态和oss服务器一系列图片目标地址list
     */
    @RequestMapping(value = "publishMyOpinion",method = RequestMethod.PUT)
    public Message NewOpinionInformation(
            @RequestParam("opinionText") String opinionText,
            @RequestParam("opinionPictureNum") Integer opinionPictureNum
    ){
        try{
            List<String> pictureAddress = systemService.putNewOpinionInformation(opinionText,opinionPictureNum);
            return Message.createSuc(pictureAddress);
        }catch(OpinionTextIsNullException e){
            e.printStackTrace();
            String msg = "发送失败";
            return Message.createErr(msg);
        }
    }


    /**
     * 获取关于我们
     *
     * @return 关于我们
     */
    @RequestMapping(value = "getAboutUs", method = RequestMethod.GET)
    public Message aboutUs() {
        try {
            return Message.createSuc(systemService.getAboutUs());
        } catch (NullContactUsFileException e) {
            e.printStackTrace();
            return Message.createErr("服务器繁忙");
        }
    }

    /**
     * 获取联系我们
     *
     * @return 联系我们
     */
    @RequestMapping(value = "getContactUs", method = RequestMethod.GET)
    public Message contactUs() {
        return Message.createSuc(systemService.getContactUs());
    }

    /**
     * 获取服务协议
     *
     * @return 服务协议
     */
    @RequestMapping(value = "getServiceAgreement", method = RequestMethod.GET)
    public Message serviceAgreement() {
        try {
            return Message.createSuc(systemService.getServiceAgreement());
        } catch (NullServiceAgreementException e) {
            e.printStackTrace();
            return Message.createErr("服务协议为空");
        }
    }


    /**
     * 获取反馈详情
     *
     * @param id 反馈id
     * @return 反馈详情
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "adminOpinionInformation", method = RequestMethod.GET)
    public Message adminOpinionInformation(@RequestParam("id") int id) {
        return Message.createSuc(systemService.getOpinionInformation(id));
    }

    /**
     * 批量删除反馈
     *
     * @param idList 反馈id列表
     * @return 删除成功信息
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "deleteAdminOpinion", method = RequestMethod.DELETE)
    public Message deleteAdminOpinion(
            @RequestBody List<Integer> idList
    ) {
        return Message.createSuc(systemService.deleteOpinion(idList));
    }

    /**
     * 获取用户反馈概览
     *
     * @param word     关键字
     * @param state    状态
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 反馈概览
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "opinionAdminGeneral", method = RequestMethod.GET)
    public Message postGeneral(
            @RequestParam(value = "word", required = false, defaultValue = "") String word,
            @RequestParam(value = "state", required = false, defaultValue = "") String state,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            return Message.createSuc(systemService.getOpinionGeneral(state, word, pageNo, pageSize));
        } catch (NoConsignGeneralException e) {
            e.printStackTrace();
            return Message.createErr("查询不到信息");
        }
    }



}
